package com.xfly.app.ui;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.utils.TimeUtil;
import com.ptmall.app.view.TitleBarView;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class AddyhqActivity extends BaseMvpActivity {

    TitleBarView title_bar;
    TextView time1,time2;
    Calendar calendar= Calendar.getInstance(Locale.CHINA);
    EditText m,j,ffnum;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_addyhq);
    }

    @Override
    public void initViews() {
        title_bar = findViewById(R.id.title_bar);
        time1 = findViewById(R.id.time1);
        time2 = findViewById(R.id.time2);
        m = findViewById(R.id.m);
        j = findViewById(R.id.j);
        ffnum= findViewById(R.id.ffnum);

        title_bar.setText("优惠券");
    }

    @Override
    public void initEvents() {
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        time1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    // 绑定监听器(How the parent is notified that the date is set.)
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        // 此处得到选择的时间，可以进行你想要的操作
                        time1.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);

                    }
                }
                        // 设置初始日期
                        , calendar.get(Calendar.YEAR)
                        , calendar.get(Calendar.MONTH)
                        , calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
        time2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    // 绑定监听器(How the parent is notified that the date is set.)
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        // 此处得到选择的时间，可以进行你想要的操作
                        time2.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);

                    }
                }
                        // 设置初始日期
                        , calendar.get(Calendar.YEAR)
                        , calendar.get(Calendar.MONTH)
                        , calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                add();
            }
        });
    }

    private void add(){
        Map<String,String> map = new HashMap<>();
        map.put("stime", TimeUtil.dateToStamp2(time1.getText().toString()));
        map.put("etime",TimeUtil.dateToStamp2(time2.getText().toString()));
        map.put("full",m.getText().toString());
        map.put("price",j.getText().toString());
        map.put("num",ffnum.getText().toString());
        apiDataRepository.addyhq(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("添加成功");
                finish();
            }
        });
    }
}
